﻿using Common;
using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_TravelAdd : Form
    {
        public Form_TravelAdd()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void Form_TravelAdd_Load(object sender, EventArgs e)
        {
            var departmentTypes = tse.DepartmentTypes.ToList();
            comType.DataSource = departmentTypes;
            comType.DisplayMember = "TypeName";
            comType.ValueMember = "DepartmentTypeID";                       
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("旅行社名称不可为空");
                return;
            }
            if (pm.IsTelephone(this.txtPhone.Text))
            {
                if (pm.IsAccountNumber(this.txtAccount.Text))
                {
                    var travels = tse.Departments.Where(u => u.DepartmentName == this.txtName.Text).Count();
                    if (travels == 0)
                    { 
                        Departments department = new Departments();
                        department.DepartmentTypeID = Convert.ToInt32(this.comType.SelectedValue);
                        department.DepartmentName = this.txtName.Text;
                        department.DepartmentNickName = this.txtNickName.Text;
                        department.ContactNumber = this.txtPhone.Text;
                        department.Fax = this.txtFax.Text;
                        department.Address = this.txtAddress.Text;
                        department.Bank = this.txtBank.Text;
                        department.AccountNumber = this.txtAccount.Text;
                        department.TaxNumber = this.txtTax.Text;
                        if (this.checkSign.Checked)
                        {
                            department.CanSign = 1;
                        }
                        else
                        {
                            department.CanSign = 0;
                        }
                        //tse.Configuration.ValidateOnSaveEnabled = false;
                        tse.Departments.Add(department);
                        var result = tse.SaveChanges();
                        if (result >= 1)
                        {
                            this.Close();
                            MessageBox.Show("添加成功");
                        }
                        else
                        {
                            MessageBox.Show("添加失败了!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("该旅行社已存在");
                    }
                }
                else
                {
                    MessageBox.Show("银行卡号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btn_Next_Click(object sender, EventArgs e)
        {
            if (pm.IsTelephone(this.txtPhone.Text))
            {
                if (pm.IsAccountNumber(this.txtAccount.Text))
                {
                      var travels=tse.Departments.Where(u=>u.DepartmentName==this.txtName.Text).Count();
                      if (travels == 0)
                      {
                          Departments department = new Departments();
                          department.DepartmentTypeID = Convert.ToInt32(this.comType.SelectedValue);
                          department.DepartmentName = this.txtName.Text;
                          department.DepartmentNickName = this.txtNickName.Text;
                          department.ContactNumber = this.txtPhone.Text;
                          department.Fax = this.txtFax.Text;
                          department.Address = this.txtAddress.Text;
                          department.Bank = this.txtBank.Text;
                          department.AccountNumber = this.txtAccount.Text;
                          department.TaxNumber = this.txtTax.Text;
                          if (this.checkSign.Checked)
                          {
                              department.CanSign = 1;
                          }
                          else
                          {
                              department.CanSign = 0;
                          }
                          tse.Configuration.ValidateOnSaveEnabled = false;
                          tse.Departments.Add(department);
                          var result = tse.SaveChanges();
                          if (result >= 1)
                          {
                              this.comType.SelectedIndex = -1;
                              this.txtName.Text = null;
                              this.txtNickName.Text = null;
                              this.txtPhone.Text = null;
                              this.txtFax.Text = null;
                              this.txtAddress.Text = null;
                              this.txtBank.Text = null;
                              this.txtAccount.Text = null;
                              this.txtTax.Text = null;
                              this.checkSign.Checked = false;
                          }
                          else
                          {
                              MessageBox.Show("添加失败了!");
                          }
                      }
                      else
                      {
                          MessageBox.Show("该旅行社已存在");

                      }
                }
                else
                {
                    MessageBox.Show("银行卡号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
